package mx.com.sinformex.simbel.pages.admin;

import java.util.List;

import mx.com.sinformex.simbel.db.eos.Group;
import mx.com.sinformex.simbel.db.eos.Report;
import mx.com.sinformex.simbel.db.eos.ReportGroupJoin;
import mx.com.sinformex.simbel.pages.BasicPage;

import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;

/**
 * @author Oscar Ivan Hernandez Ventura [ oscar at sinformex dot com dot mx]
 *
 * @version $ Id : GroupReport.java , v 1.0 12/05/2009 angellore $
 */
public class GroupReport extends BasicPage {
	
	@Persist
	private Group group;
	@Property 
	private Report report;
	
	public List<Report> getReports() {
		return getAdminDataAccess().fetchReports();
	}
	
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	
	public void setHaveReport(boolean have) {
		ReportGroupJoin join = getAdminDataAccess().getReportGroupJoin(group.getGroupId(), report.getReportId());
		if(have) {
			if(join == null) {
				join = new ReportGroupJoin();
				join.setGroup(group.getGroupId());
				join.setReport(report.getReportId());
				getAdminDataAccess().addReportGroupJoin(join);
			}
		} else {
			if(join != null) {
				getAdminDataAccess().deleteReportGroupJoin(join);
			}
		}
	}
	
	public boolean getHaveReport() {
		return getAdminDataAccess().getReportGroupJoin(group.getGroupId(), report.getReportId()) != null;
	}
	
	Object onSuccess() {
		if(getError() != null) {
			return null;
		}
		
		if(group != null) {
			group = getAdminDataAccess().getGroup(group.getGroupId());
			return "admin/groups";
		}
		return null;
	}
	
	
}
